package com.miracle.java2110.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.miracle.java2110.common.PageVo;
import com.miracle.java2110.common.R;
import com.miracle.java2110.mapper.EmployeemanagerMapper;
import com.miracle.java2110.pojo.Employeemanager;
import com.miracle.java2110.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @author miracle
 * @date 2021/11/8 20:21
 * @Description
 */
@Service("employeeService")
public class EmployeeServiceImpl implements EmployeeService {

    @Autowired
    private EmployeemanagerMapper employeemanagerMapper;


    @Override
    public PageVo<Employeemanager> getPageData(Map<String, Object> params) {
        //mybatis 分页插件的三个过程
        PageHelper.startPage(Integer.parseInt(params.get("page")+""),Integer.parseInt(params.get("limit")+""));
        // List<User> users = userMapper.selectByExample(null);
        List<Employeemanager> employeemanagers = employeemanagerMapper.selectByParams(params);

        PageInfo<Employeemanager> pageInfo = new PageInfo<>(employeemanagers);
        //把分页数据 封装到自定义的分页对象中 因为layui 要求的是json格式数据
        PageVo<Employeemanager> employPageVo = new PageVo<>();
        employPageVo.setCode(0);
        employPageVo.setMsg("查询用户分页数据");
        employPageVo.setCount(pageInfo.getTotal());
        employPageVo.setData(pageInfo.getList());

        return employPageVo;
    }

    @Override
    public R deleteById(Integer id) {
        int result = employeemanagerMapper.deleteByPrimaryKey(id);
        if (result==1){
            return R.ok(200,"删除成功");
        }else {
            return  R.error(400,"删除失败");
        }
    }

    @Override
    public R updateById(Employeemanager employeemanager) {
        int result = employeemanagerMapper.updateByPrimaryKeySelective(employeemanager);

        if (result == 1){
            return  R.ok(200,"添加成功");
        }else{
            return R.error(400,"添加失败");
        }
    }
    @Override
    public R addEmployee(Employeemanager employeemanager) {
        int result = employeemanagerMapper.insertSelective(employeemanager);
        if(result!=0){
            return R.ok(200,"添加成功");
        }else {
            return R.error(400,"添加失败");
        }
    }
    @Override
    public List<Employeemanager> selectAllEmployeemanager(){

        List<Employeemanager> employeemanagers = employeemanagerMapper.selectAllEmployeemanger();

        return employeemanagers;

    }
}
